package com.charity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charity.domain.Project;
import com.charity.domain.Volunteer;
import com.charity.service.IVolunteerService;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 志愿者
 */
@RestController
@RequestMapping("/volunteer")
public class VolunteerController {

    @Resource
    private IVolunteerService volunteerService;

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody Volunteer volunteer) {
        Map<String, Object> response = new HashMap<>();
        if (volunteerService.register(volunteer)) {
            response.put("code", 200);
            response.put("message", "志愿者注册成功，等待审核");
        } else {
            response.put("code", 400);
            response.put("message", "注册失败，请检查信息");
        }
        return ResponseEntity.ok(response);
    }

    @PutMapping("/audit/{volunteerId}")
    public ResponseEntity<Map<String, Object>> auditVolunteer(
            @PathVariable Long volunteerId,
            @RequestParam Integer status,
            @RequestParam(required = false) String remark) {
        Map<String, Object> response = new HashMap<>();
        if (volunteerService.auditVolunteer(volunteerId, status, remark)) {
            response.put("code", 200);
            response.put("message", "审核完成");
        } else {
            response.put("code", 400);
            response.put("message", "审核失败");
        }
        return ResponseEntity.ok(response);
    }

    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getVolunteerList(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> response = new HashMap<>();
        Page<Volunteer> volunteers = volunteerService.getVolunteerList(status, pageNum, pageSize);
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", volunteers);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/detail/{volunteerId}")
    public ResponseEntity<Map<String, Object>> getVolunteerDetail(@PathVariable Long volunteerId) {
        Map<String, Object> response = new HashMap<>();
        Volunteer volunteer = volunteerService.getVolunteerById(volunteerId);
        if (volunteer != null) {
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", volunteer);
        } else {
            response.put("code", 404);
            response.put("message", "志愿者不存在");
        }
        return ResponseEntity.ok(response);
    }

    @PostMapping("/join/{projectId}")
    public ResponseEntity<Map<String, Object>> joinProject(
            @PathVariable Long projectId,
            @RequestParam Long volunteerId) {
        Map<String, Object> response = new HashMap<>();
        if (volunteerService.joinProject(volunteerId, projectId)) {
            response.put("code", 200);
            response.put("message", "成功参与项目");
        } else {
            response.put("code", 400);
            response.put("message", "参与失败");
        }
        return ResponseEntity.ok(response);
    }

    @GetMapping("/projects/{volunteerId}")
    public ResponseEntity<Map<String, Object>> getVolunteerProjects(
            @PathVariable Long volunteerId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> response = new HashMap<>();
        Page<Project> projects = volunteerService.getVolunteerProjects(volunteerId, pageNum, pageSize);
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", projects);
        return ResponseEntity.ok(response);
    }
}